package com.ming.core.util;

public class ThreadLocalDemo {
    // ThreadLocal一般权限声明为private static
    private static final ThreadLocal<Person> aPerson = new ThreadLocal<Person>() {

        // 默认返回null，在此override初始化方法
        @Override
        protected Person initialValue() {
            return new Person("A Baby", 0);
        }
    }; 
    
    
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(new MyTask(true, aPerson), "Henry");
        Thread t2 = new Thread(new MyTask(true, aPerson), "Cevin");
        // 该线程不设置ThreadLocal，直接使用初始值
        Thread t3 = new Thread(new MyTask(false, aPerson), "Jessica");
        t1.start();
        t2.start();
        t3.start();
    }
} /* Output:
Jessica Start......
Henry Start......
Cevin Start......
This thread's ThreadLocal: A Baby-0
This thread's ThreadLocal: Cevin-24
This thread's ThreadLocal: Henry-12
*///:~
